#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

MY_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Bash sanity settings (error on exit, complain for undefined vars, error when pipe fails)
set -euo pipefail

CMDNAME="$(basename -- "$0")"

if [[ ${AIRFLOW_CI_VERBOSE:="false"} == "true" ]]; then
    set -x
fi

if [[ -z "${AIRFLOW__CORE__SQL_ALCHEMY_CONN:=}" ]]; then
    echo "AIRFLOW__CORE__SQL_ALCHEMY_CONN not set - using default" >&2
fi

# Update short and long options in the breeze-complete script
# This way autocomplete will work automagically with all options
# shellcheck source=run-tests-complete
. "${MY_DIR}/run-tests-complete"


# environment
export AIRFLOW_HOME=${AIRFLOW_HOME:=${HOME}}
AIRFLOW_SOURCES="$(cd "${MY_DIR}" && pwd)"
export AIRFLOW_SOURCES
export AIRFLOW__CORE__DAGS_FOLDER="S{AIRFLOW_SOURCES}/tests/dags"
export AIRFLOW__CORE__UNIT_TEST_MODE=True

# add test/test_utils to PYTHONPATH TODO: Do we need that ??? Looks fishy.
export PYTHONPATH=${PYTHONPATH:=}:${AIRFLOW_SOURCES}/tests/test_utils

# make sure the default AWS_REGION is set
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:='us-east-1'}

echo Airflow home: "${AIRFLOW_HOME}"
echo Airflow root: "${AIRFLOW_SOURCES}"
echo Home of the user: "${HOME}"

export AIRFLOW__CORE__DAGS_FOLDER="${AIRFLOW_SOURCES}/tests/dags"

usage() {
      echo """

Usage: ${CMDNAME} [FLAGS] [TESTS_TO_RUN] -- <EXTRA_NOSETEST_ARGS>

Runs tests specified (or all tests if no tests are specified)

Flags:

-h, --help
        Shows this help message.

-i, --with-db-init
        Forces database initialization before tests

-x, --with-xunit
        Dumps result of the tests to Xunit file

-f, --xunit-file
        The file where xunit results should be dumped. Default if not specified
        is ${AIRFLOW_SOURCES}/logs/all_tests.xml

-s, --nocapture
        Don't capture stdout when running the tests. This is useful if you are
        debugging with ipdb and want to drop into console with it
        by adding this line to source code:

            import ipdb; ipdb.set_trace()

-v, --verbose
        Verbose output showing coloured output of tests being run and summary
        of the tests - in a manner similar to the tests run in the CI environment.

"""
}

echo

####################  Parsing options/arguments
if ! PARAMS=$(getopt \
    -o "${_RUN_TESTS_GETOPT_SHORT_OPTIONS:=}" \
    -l "${_RUN_TESTS_GETOPT_LONG_OPTIONS:=}" \
    --name "$CMDNAME" -- "$@")
then
    usage
fi

eval set -- "${PARAMS}"
unset PARAMS

WITH_DB_INIT="false"
NOCAPTURE="false"
WITH_XUNIT="false"
XUNIT_FILE="${AIRFLOW_SOURCES}/logs/all_tests.xml"
VERBOSE="${AIRFLOW_CI_VERBOSE}"

# Parse Flags.
# Please update short and long options in the run-tests-complete script
# This way autocomplete will work out-of-the-box
while true
do
  case "${1}" in
    -h|--help)
      usage;
      exit 0 ;;
    -i|--with-db-init)
      WITH_DB_INIT="true"
      shift ;;
    -s|--nocapture)
      NOCAPTURE="true"
      shift ;;
    -x|--with-xunit)
      WITH_XUNIT="true"
      shift ;;
    -v|--verbose)
      VERBOSE="true"
      shift;;
    -f|--xunit-file)
      XUNIT_FILE="$2"
      shift; shift ;;
    --)
      shift ;
      break ;;
    *)
      usage
      echo
      echo "ERROR: Unknown argument ${1}"
      echo
      exit 1
      ;;
  esac
done


# any argument received after -- is overriding the default nose execution arguments:
NOSE_ARGS=("$@")

AIRFLOW_DB_INITIALISED_FILE=${HOME}/.airflow_db_initialised

if [[ "${WITH_DB_INIT}" == "true" || ! -f ${AIRFLOW_DB_INITIALISED_FILE} ]]; then
    echo
    if [[ "${WITH_DB_INIT}" == "true" ]]; then
        echo "Initializing the DB - forced with --with-db-init switch"
    else
        echo "Initializing the DB - first time after entering the container"
        echo "You can force re-initialization the database by adding --with-db-init switch to run-tests."
    fi
    echo
    yes | airflow db init || true
    airflow db reset -y
    touch "${AIRFLOW_DB_INITIALISED_FILE}"
else
    echo
    echo "Skipping initializing of the DB as it was initialized already"
    echo
    echo "You can re-initialize the database by adding --with-db-init flag when running tests"
    echo
fi

if [[ "${KRB5_KTNAME:=}" == "" ]]; then
    echo "KRB5_KTNAME variable is empty - no kerberos intialisation"
else
    kinit -kt "${KRB5_KTNAME}" airflow
fi

if [[ "${#NOSE_ARGS[@]}" == "0" ]]; then
  NOSE_ARGS=("--with-coverage"
            "--cover-erase"
            "--cover-html"
            "--cover-package=airflow"
            "--cover-html-dir=airflow/www/static/coverage"
            "--with-ignore-docstrings"
            "--rednose"
            "--with-timer"
            "-v"
            "--logging-level=DEBUG")
fi

if [[ "${WITH_XUNIT}" == "true" ]]; then
    echo
    echo "Dumping results to ${XUNIT_FILE}"
    echo
    NOSE_ARGS+=("--with-xunit" "--xunit-file=${XUNIT_FILE}")
fi

if [[ "${NOCAPTURE}" == "true" ]]; then
    echo
    echo "Stop capturing stdout"
    echo
    NOSE_ARGS+=("--nocapture")
fi

if [[ "${VERBOSE}" == "true" ]]; then
    echo
    echo "Verbose output"
    echo
    NOSE_ARGS+=("--rednose" "--with-timer" "-v" "--logging-level=DEBUG")
fi

echo
echo "Starting the tests with arguments: ${NOSE_ARGS[*]}"
echo
nosetests "${NOSE_ARGS[@]}"
